﻿using System;
using System.Xml;
using System.Collections.Generic;

namespace BfLib.Web.RSS
{
    public class RSSItem
    {
        private string _title, _link, _description, _author, _comments, _pubDate;
        private RSSItemSource _source;
        private RSSItemEnclosure _enclosure;
        private RSSCategoryList _catagories;
        private RSSItemGuid _guid;

        public RSSItem(XmlNode node)
        {

            XmlNode n;

            n = node.SelectSingleNode("title");
            if (n == null)
                throw new Exception("<item> element does not contain the mandatory <title> element");
            else
            {
                if (n.FirstChild == null)
                {
                    this._title = n.Value;
                }
                else
                {
                    this._title = n.ChildNodes[0].Value;
                }
            }

            n = node.SelectSingleNode("link");
            if (n == null)
                throw new Exception("<item> element does not contain the mandatory <link> element");
            else
            {
                if (n.FirstChild == null)
                {
                    this._link = n.Value;
                }
                else
                {
                    this._link = n.ChildNodes[0].Value;
                }
            }

            n = node.SelectSingleNode("description");
            if (n == null)
                throw new Exception("<item> element does not contain the mandatory <description> element");
            else
            {
                if (n.FirstChild == null)
                {
                    this._description = n.Value;
                }
                else
                {
                    this._description = n.ChildNodes[0].Value;
                }
            }


            n = node.SelectSingleNode("author");
            if (n != null)
            {
                if (n.FirstChild == null)
                {
                    this._author = n.Value;
                }
                else
                {
                    this._author = n.ChildNodes[0].Value;
                }
            }

            n = node.SelectSingleNode("category");
            if (n != null)
                this._catagories = new RSSCategoryList(n);
            

            n = node.SelectSingleNode("comments");
            if (n != null)
            {
                if (n.FirstChild == null)
                {
                    this._comments = n.Value;
                }
                else
                {
                    this._comments = n.ChildNodes[0].Value;
                }
            }

            n = node.SelectSingleNode("enclosure");
            this._enclosure = new RSSItemEnclosure(n);

            n = node.SelectSingleNode("guid");
            this._guid = new RSSItemGuid(n);

            n = node.SelectSingleNode("pubDate");
            if (n != null)
            {
                if (n.FirstChild == null)
                {
                    this._pubDate = n.Value;
                }
                else
                {
                    this._pubDate = n.ChildNodes[0].Value;
                }
            }

            n = node.SelectSingleNode("source");
            this._source = new RSSItemSource(n);

        }

        #region Public accessors
        public RSSItemSource Source
        {
            get { return _source; }
        }

        public string PubDate
        {
            get { return _pubDate; }
        }

        public RSSItemGuid Guid
        {
            get { return _guid; }
        }

        public RSSItemEnclosure Enclosure
        {
            get { return _enclosure; }
        }

        public string Comments
        {
            get { return _comments; }
        }

        public RSSCategoryList Category
        {
            get { return _catagories; }
        }

        public string Author
        {
            get { return _author; }
        }

        public string Description
        {
            get { return _description; }
        }

        public string Link
        {
            get { return _link; }
        }

        public string Title
        {
            get { return _title; }
        }

        #endregion
    }

}
